package jonaTextGen;

import Jona.*;

public class GenerateText{

	//String totaleOutput = "The issue under investigation is the following: \n";
	int niveau = 1;
	Theory theory;
	String textGenModus;
	
	// er moeten ook operaties qua volgorde gedaan kunnen worden.	
	
	public GenerateText(Theory theory){
		this.theory = theory;
	}
	
	public void setTheory(Theory theory, String textGenModus)
	{
		this.theory = theory;
		if(textGenModus.equalsIgnoreCase("A"))
			this.textGenModus = "A";
		if(textGenModus.equalsIgnoreCase("B"))
			this.textGenModus = "B";
		niveau = 1;
	}
	
	public String outputText(){
		//	hier de text maken aan de hand van de volgorde
		ArgObject[] currentTheory = theory.giveRootStatements();
		if(textGenModus.equals("A"))
		{
			return totaleTextA(currentTheory);
		}
		else if(textGenModus.equals("B"))
		{
			return totaleTextB(currentTheory);
		}
		else return "error in text";

		// theory.getValue() gebruiken om te kijken wat wat ontkracht

	}	
	
	public String totaleTextA(ArgObject[] argObjects)
	{
		niveau++;
		String rt = "";
		ArgObject[] attackers;
		ArgObject[] supporters;
		for(int i=0; i < argObjects.length; i++)
		{	
			if(argObjects[i] instanceof Conditional) 
			{ 

				Conditional c1 = (Conditional) argObjects[i];
				ArgObject[] antecedents = c1.getAntecedents();
				rt += totaleTextA(antecedents);
			}
			else
			{
				ArgObject[] roots = theory.giveRootStatements();
				for(int x=0; x < roots.length; x++)
				{
					if(argObjects[i] == roots[x])
					{
						//rt += "Text Generation mode: " + textGenModus + "\n";
						rt += "The issue under investigation claims the following is true: \n";
					}
				}
				for(int bla=1; bla < niveau; bla++)
				{
					rt += "        ";
				}
				//rt += "(" + niveau + ")" + argObjects[i].getText() + "\n";
				rt += argObjects[i].getText() + "\n";
				niveau--;
			}

			ArgObject[] statements = theory.getStatements();
			ArgObject[][] attacksupport = getAttackerAndSupporters(argObjects[i],statements);
			int totalA = attacksupport[0].length; 
			int totalB = attacksupport[1].length;
			//int total = totalA+totalB;
			attackers = new ArgObject[totalA];
			supporters = new ArgObject[totalB];
			
			for(int bla=0; bla < totalA; bla++)
			{
				attackers[bla] = attacksupport[0][bla];
			}
			for(int bla=0; bla < totalB; bla++)
			{
				supporters[bla] = attacksupport[1][bla];
			}

			if(supporters.length > 0)
			{
				for(int bla=0; bla <= niveau; bla++)
				{
					rt += "        ";
				}
				//rt += "Statement (" + niveau + ") is supported by the fact that... \n" + totaleText(supporters);
				rt += "This is supported by the fact that... \n";
				rt += totaleTextA(supporters);
				niveau--;
			}
			if(attackers.length > 0)
			{
				for(int bla=0; bla <= niveau; bla++)
				{
					rt += "        ";
				}
				rt += "But... \n"; 
				rt += totaleTextA(attackers);
				niveau--;
			}
			
		}
		return rt;
	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public String totaleTextB(ArgObject[] argObjects)
	{
		niveau++;
		String rt = "";
		ArgObject[] attackers;
		ArgObject[] supporters;
		for(int i=0; i < argObjects.length; i++)
		{	
			if(argObjects[i] instanceof Conditional) 
			{ 

				Conditional c1 = (Conditional) argObjects[i];
				ArgObject[] antecedents = c1.getAntecedents();
				rt += totaleTextA(antecedents);
			}
			else
			{
				ArgObject[] roots = theory.giveRootStatements();
				for(int x=0; x < roots.length; x++)
				{
					if(argObjects[i] == roots[x])
					{
						//rt += "Text Generation mode: " + textGenModus + "\n";
						rt += "The issue under investigation claims the following is true: \n";
					}
				}
				for(int bla=1; bla < niveau; bla++)
				{
					rt += "        ";
				}
				//rt += "(" + niveau + ")" + argObjects[i].getText() + "\n";
				rt += "(" + niveau + ")" + argObjects[i].getText() + "\n";
			}

			ArgObject[] statements = theory.getStatements();
			ArgObject[][] attacksupport = getAttackerAndSupporters(argObjects[i],statements);
			int totalA = attacksupport[0].length; 
			int totalB = attacksupport[1].length;
			//int total = totalA+totalB;
			attackers = new ArgObject[totalA];
			supporters = new ArgObject[totalB];
			
			for(int bla=0; bla < totalA; bla++)
			{
				attackers[bla] = attacksupport[0][bla];
			}
			for(int bla=0; bla < totalB; bla++)
			{
				supporters[bla] = attacksupport[1][bla];
			}

			if(supporters.length > 0)
			{
				for(int bla=0; bla <= niveau; bla++)
				{
					rt += "        ";
				}
				//rt += "Statement (" + niveau + ") is supported by the fact that... \n" + totaleText(supporters);
				rt += "This is supported by the fact that... \n" + totaleTextA(supporters);
			}
			if(attackers.length > 0)
			{
				for(int bla=0; bla <= niveau; bla++)
				{
					rt += "        ";
				}
				rt += "But... \n" + totaleTextA(attackers);
			}
			
		}
		niveau--;
		//return rt;
		return "Test";
	}
	
	public ArgObject[][] getAttackerAndSupporters(ArgObject root, ArgObject[] statements)
	{
		ArgObject[][] attacksupport = new ArgObject[2][0];
		
		for(int i =0 ; i < statements.length; i++)
		{
			if(statements[i] instanceof Conditional)
			{
				Conditional c1 = (Conditional)statements[i];
				if(c1.getConsequent() instanceof Negation)
				{
					Negation n1 = (Negation)c1.getConsequent();
					if(n1.getNegation() == root)
						attacksupport[0] = addToList(attacksupport[0],c1);	
				}
				else
				{
					if(c1.getConsequent() == root)
						attacksupport[1] = addToList(attacksupport[1],c1);
				}
			}
		}
		
		return attacksupport;
	}
	
	protected ArgObject [] addToList(ArgObject [] list, ArgObject element) {
		if(list == null)  
		{
			list = new ArgObject[1];
			list[0] = element;
			return list;
		}
		else
		{
			int i, length;
			ArgObject [] temp;
			
			length = list.length;
			
			temp = new ArgObject[length+1];
			for (i = 0; i < length; ++i) {
				if (list[i] == element) {
					return list;
				}
				temp[i] = list[i];
			}
			temp[length] = element;
			return temp;
		}
	}
	
	
}